﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElementumForever.Interfaces;
using ElementumForever.Utility;
using ElementumForever.Scene;

namespace ElementumForever.Editer
{
    public class EntitySerializer
    {
        public static EntityInfo SerializeEntity(IEntity entity)
        {
            EntityInfo data = new EntityInfo(entity.Name, true);

            data.AddElement(LuaConstants.Name, entity.Name);
            data.AddElement(LuaConstants.Rotation, entity.Rotation);
            data.AddElement(LuaConstants.Width, entity.Width);
            data.AddElement(LuaConstants.Height, entity.Height);

            data.AddElement(LuaConstants.ScaleX, entity.ScaleX);
            data.AddElement(LuaConstants.ScaleY, entity.ScaleY);

            data.AddElement(LuaConstants.OriginX, entity.OriginX);
            data.AddElement(LuaConstants.OriginY, entity.OriginY);

            data.AddElement(LuaConstants.LayerDepth, entity.LayerDepth);
            data.AddElement(LuaConstants.Color, entity.Color);
            data.AddElement(LuaConstants.ObjectClass, entity.ObjectClass);

            data.AddElement(LuaConstants.X, entity.X);
            data.AddElement(LuaConstants.Y, entity.Y);

            EntityInfo children = new EntityInfo(LuaConstants.Children, true);
            data.AddElement(children);
            foreach (BaseEntity child in entity.Children)
            {
                EntityInfo _data = child.Serialize();
                if (_data != null && _data.Name != null && _data.Name != "")
                    children.AddElement(_data);
            }

            return data;
        }

        public static EntityInfo SerializeScene(BaseScene entity)
        {
            EntityInfo _sceneData = SerializeEntity(entity);

            _sceneData.AddElement(LuaConstants.Zoom, entity.Zoom);
            _sceneData.AddElement(LuaConstants.CenterX, entity.CenterX);
            _sceneData.AddElement(LuaConstants.CenterY, entity.CenterY);
            _sceneData.AddElement(LuaConstants.HasPhysics, true);

            EntityInfo backgroundProperties = new EntityInfo(LuaConstants.BackgroundProperties, true);
            EntityInfo physicalProperties = new EntityInfo(LuaConstants.PhysicalProperties, true);

            backgroundProperties.AddElement(LuaConstants.Color, entity.Color);

            physicalProperties.AddElement(LuaConstants.GravityX, entity.GravityX);
            physicalProperties.AddElement(LuaConstants.GravityY, entity.GravityY);
            physicalProperties.AddElement(LuaConstants.DoSleep, false);

            _sceneData.AddElement(backgroundProperties);
            _sceneData.AddElement(physicalProperties);

            return _sceneData;
        }

        public static EntityInfo SerializeCameraScene(CameraScene scene) {
            EntityInfo _sceneInfo = SerializeScene(scene);
            EntityInfo cameraInfo = SerializeCamera(scene.Camera);
            _sceneInfo.AddElement(cameraInfo);
            return _sceneInfo;
        }

        public static EntityInfo SerializeCamera(ICamera camera)
        {
            EntityInfo cameraInfo = new EntityInfo(LuaConstants.Camera, true);
            cameraInfo.AddElement(LuaConstants.X,camera.X);
            cameraInfo.AddElement(LuaConstants.Y, camera.Y);
            cameraInfo.AddElement(LuaConstants.ScaleX, camera.ScaleX);
            cameraInfo.AddElement(LuaConstants.ScaleY, camera.ScaleY);
            cameraInfo.AddElement(LuaConstants.Rotation, camera.Rotation);
            return cameraInfo;
        }

        public static System.Drawing.Color ToColorForm(EntityInfo colorData)
        {
            byte r = Convert.ToByte(colorData.Table["r"].Value);
            byte g = Convert.ToByte(colorData.Table["g"].Value);
            byte b = Convert.ToByte(colorData.Table["b"].Value);
            return UnitHelper.ToColorForm(r, g, b);
        }

    }
}
